cargo clean. resolves #51
authorJoshua DeSeno <jdeseno@gmail.com>
Mon, 7 Jul 2014 08:50:05 +0000 (17:50 +0900)
committerjosh <jdeseno@gmail.com>
Tue, 8 Jul 2014 12:54:15 +0000 (21:54 +0900)
Makefile
src/bin/cargo-clean.rs [new file with mode: 0644]
src/cargo/ops/cargo_clean.rs [new file with mode: 0644]
src/cargo/ops/mod.rs
tests/support/mod.rs
tests/test_cargo_clean.rs [new file with mode: 0644]
tests/tests.rs

index d1954d2e580e5dc5300593258d93aee4dafc6c94..68c48c0382282abb6d279e98e067bebf4de1c50b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -14,6 +14,7 @@ export PATH := $(PATH):$(CURDIR)/rustc/bin
 # Link flags to pull in dependencies
 BINS = cargo \
             cargo-build \
+            cargo-clean \
             cargo-read-manifest \
             cargo-rustc \
             cargo-verify-project \
diff --git a/src/bin/cargo-clean.rs b/src/bin/cargo-clean.rs
new file mode 100644 (file)
index 0000000..7d9189c
--- /dev/null
@@ -0,0 +1,48 @@
+#![crate_id="cargo-clean"]
+#![feature(phase)]
+
+extern crate cargo;
+
+#[phase(plugin, link)]
+extern crate hammer;
+
+#[phase(plugin, link)]
+extern crate log;
+
+extern crate serialize;
+
+use std::os;
+use cargo::ops;
+use cargo::{execute_main_without_stdin};
+use cargo::core::MultiShell;
+use cargo::util::{CliResult, CliError};
+use cargo::util::important_paths::find_project_manifest;
+
+#[deriving(PartialEq,Clone,Decodable,Encodable)]
+pub struct Options {
+    manifest_path: Option<String>
+}
+
+hammer_config!(Options)
+
+fn main() {
+    execute_main_without_stdin(execute);
+}
+
+fn execute(options: Options, _shell: &mut MultiShell) -> CliResult<Option<()>> {
+    debug!("executing; cmd=cargo-clean; args={}", os::args());
+
+    let root = match options.manifest_path {
+        Some(path) => Path::new(path),
+        None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml")
+                    .map_err(|_| {
+                        CliError::new("Could not find Cargo.toml in this \
+                                       directory or any parent directory",
+                                      102)
+                    }))
+    };
+
+    ops::clean(&root).map(|_| None).map_err(|err| {
+      CliError::from_boxed(err, 101)
+    })
+}
diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs
new file mode 100644 (file)
index 0000000..2d67885
--- /dev/null
@@ -0,0 +1,21 @@
+use std::io::fs::{rmdir_recursive};
+use core::{SourceId};
+use util::{CargoResult, human, ChainError};
+use ops::{read_manifest};
+use std::io::{File};
+use util::toml::{project_layout};
+
+pub fn clean(path: &Path) -> CargoResult<()>
+{
+    let mut file = try!(File::open(path));
+    let data = try!(file.read_to_end());
+    let layout = project_layout(&path.dir_path());
+    let (manifest, _) = try!(read_manifest(data.as_slice(), layout, &SourceId::for_path(path)));
+    let build_dir = manifest.get_target_dir();
+
+    if build_dir.exists() {
+      return rmdir_recursive(build_dir).chain_error(|| human("Could not remove build directory"))
+    }
+
+    Ok(())
+}
index 45ec38101f63e78b4e4365d12e8981ec6073e879..d47b136201325296e055bb4445a6601f18610339 100644 (file)
@@ -1,7 +1,9 @@
+pub use self::cargo_clean::clean;
 pub use self::cargo_compile::{compile, CompileOptions};
 pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages};
 pub use self::cargo_rustc::compile_targets;
 
+mod cargo_clean;
 mod cargo_compile;
 mod cargo_read_manifest;
 mod cargo_rustc;
index 49ab97f93a58e5f0d40e7a68e8c35e233b0dea97..9dfcd696ba9b24e26f206ff2ce60666a7c620a1f 100644 (file)
@@ -98,7 +98,11 @@ impl ProjectBuilder {
     }
 
     pub fn bin(&self, b: &str) -> Path {
-        self.root.join("target").join(format!("{}{}", b, os::consts::EXE_SUFFIX))
+        self.build_dir().join(format!("{}{}", b, os::consts::EXE_SUFFIX))
+    }
+
+    pub fn build_dir(&self) -> Path {
+        self.root.join("target")
     }
 
     pub fn process<T: ToCStr>(&self, program: T) -> ProcessBuilder {
diff --git a/tests/test_cargo_clean.rs b/tests/test_cargo_clean.rs
new file mode 100644 (file)
index 0000000..4e4c13e
--- /dev/null
@@ -0,0 +1,17 @@
+use support::{project, execs, main_file, basic_bin_manifest};
+use hamcrest::{assert_that, existing_dir, is_not};
+
+fn setup() {
+}
+
+test!(cargo_clean_simple {
+    let p = project("foo")
+              .file("Cargo.toml", basic_bin_manifest("foo").as_slice())
+              .file("src/foo.rs", main_file(r#""i am foo""#, []).as_slice());
+
+    assert_that(p.cargo_process("cargo-build"), execs());
+    assert_that(&p.build_dir(), existing_dir());
+
+    assert_that(p.cargo_process("cargo-clean"), execs());
+    assert_that(&p.build_dir(), is_not(existing_dir()));
+})
index 6dd3bb2802267706167a7ca6268fae465c3544d5..bb5bf97395436a1970c0b022b21bc4b1010f12e7 100644 (file)
@@ -20,6 +20,7 @@ macro_rules! test(
     )
 )
 
+mod test_cargo_clean;
 mod test_cargo_compile;
 mod test_cargo_compile_git_deps;
 mod test_cargo_compile_path_deps;